package me.zd.tool.hardlink.view.listener;

import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.JTextComponent;
import java.util.function.Consumer;

public class TextChangedListener implements DocumentListener {
    private final JTextComponent component;
    private final Consumer<String> changedOperation;
    private final ModelLock modelLock;

    public TextChangedListener(JTextComponent component, Consumer<String> changedOperation, ModelLock modelLock) {
        this.component = component;
        this.changedOperation = changedOperation;
        this.modelLock = modelLock;
    }

    @Override
    public void insertUpdate(DocumentEvent e) {
        changedUpdate(e);
    }

    @Override
    public void removeUpdate(DocumentEvent e) {
        changedUpdate(e);
    }

    @Override
    public void changedUpdate(DocumentEvent e) {
        if (!modelLock.isLocked()) {
            changedOperation.accept(component.getText());
        }
    }
}
